/*
** FileName:1738.c
** Author:Creat
** Time:下午3:23
** Date:2024/5/26
**/

#include <string.h>
#include "PriorityQueue.h"
int cmp_1738(void *p1, void *p2){
    if(p1 == NULL && p2 == NULL)
        return 0;
    else if(p1 == NULL)
        return 1;
    else if(p2 == NULL)
        return -1;
    else
        return *(int *)p2 - *(int*)p1;
}
int kthLargestValue(int** matrix, int matrixSize, int* matrixColSize, int k) {
    PriorityQueue *pq = NULL;
    int m = matrixSize, n = *matrixColSize;
    init(&pq, m * n, cmp_1738);
    int pre[m + 1][n + 1];
    memset(pre, 0, sizeof(pre));
    for(int i = 1;i <= m;i++)
        for(int j = 1;j <= n;j++){
            pre[i][j] = pre[i - 1][j] ^ pre[i][j - 1] ^ pre[i-1][j-1] ^ matrix[i - 1][j - 1];
            push(pq, &pre[i][j]);
        }
    int res = 0;
    for(int i = 0;i < k;i++)
        res = *(int*)pop(pq);
    destroy(pq);
    return res;
}

//int main(void){
//    int datas[] = {5,34,7,2,6,4,2};
//    int datas_size = 7;
//    PriorityQueue *pq = NULL;
//    init(&pq, 10, cmp);
//    for(int i = 0;i < datas_size;i++){
//        push(pq, (void*)&datas[i]);
//    }
//    for(int i = 0;i < datas_size;i++){
//        printf("%d ", *(int *)pop(pq));
//    }
//    destroy(pq);
//    return 0;
//}